<template>
  <el-container>
    <el-header
      style="background-color: #363636; height: 60px; padding-top: 10px"
    >
      <el-row>
        <el-button
          type="text"
          @click="home()"
          style="
            color: #409eff;
            float: left;
            margin-left: 20px;
            font-size: 20px;
          "
          >Home</el-button
        >
        <el-button
          type="text"
          @click="logout()"
          style="color: red; float: right; margin-right: 20px"
          >注销</el-button
        >
      </el-row>
    </el-header>
    <el-main
      style="width: 50%; margin-left: 25%; height: 90vh; margin-top: 100px"
    >
      <el-row>
        <el-col :span="8">
          <el-upload
            class="avatar-uploader"
            :action="uploadAction"
            :show-file-list="false"
            :on-success="handleGoodSuccess"
          >
            <img v-if="goodImage" :src="goodImage" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            <div class="el-upload__tip" slot="tip">
              点击上传商品封面
            </div></el-upload
          ></el-col
        >
        <el-col :span="16" style="text-align: left">
          <el-select v-model="goodType" placeholder="商品类别">
            <el-option
              v-for="item in goodTypes"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
          <el-input
            style="margin-top: 20px"
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 4 }"
            placeholder="请输入商品标题"
            v-model="goodTitle"
          >
          </el-input>
          <el-input
            style="margin-top: 20px"
            placeholder="请输入商品价格(元)"
            v-model="goodPrice"
          >
          </el-input
        ></el-col>
      </el-row>
      <el-row>
        <div style="text-align: left; margin-top: 50px">
          <span style="font-size: 20px; font-style: inherit"> 商品详情</span>
          <div
            style="
              border: 1px #333333;
              border-radius: 6px;
              padding: 1px;
              width: 100%;
              margin-top: 20px;
            "
          >
            <el-upload
              :action="uploadAction"
              list-type="picture-card"
              :on-preview="handleDetailPreview"
              :on-remove="handleRemoveDetail"
              :on-success="handleDetailSuccess"
            >
              <i class="el-icon-plus"></i>
            </el-upload>
            <el-dialog :visible.sync="dialogVisible">
              <img width="100%" :src="dialogImageUrl" alt="" />
            </el-dialog>
          </div>
        </div>
      </el-row>
      <el-row>
        <el-button
          type="primary"
          style="margin-top: 50px; float: right; font-size: 18px"
          @click="addGood()"
          >添加商品</el-button
        >
      </el-row>
    </el-main>
  </el-container>
</template>
    
    <script>
import store from "../store";

export default {
  name: "Admin",
  data() {
    return {
      goodType: "",
      goodTypes: [
        {
          label: "冰箱",
          value: 1,
        },
        {
          label: "洗衣机",
          value: 4,
        },
        {
          label: "空调",
          value: 3,
        },
        {
          label: "电视",
          value: 2,
        },
      ],
      goodImage: null,
      dialogVisible: false,
      dialogImageUrl: "",
      goodTitle: "",
      goodPrice: "",
      uploadAction: "http://localhost:8080/posts",
      detailList: [],
    };
  },
  mounted: function () {},
  methods: {
    reset() {
      this.goodTitle = "";
      this.goodImage = "";
      this.goodType = "";
      this.goodPrice = "";
      this.detailList.splice(0, this.detailList.length);
    },
    addGood() {
      if (!this.goodTitle) {
        alert("输入商品名称");
        return;
      }
      if (!this.goodPrice) {
        alert("输入商品价格");
        return;
      }
      if (!this.goodImage) {
        alert("上传商品封面");
        return;
      }
      var details = this.detailList.join(",");
      console.log(details);
      this.$apiFun
        .addGood({
          goodTitle: this.goodTitle,
          goodImage: this.goodImage,
          goodType: this.goodType,
          goodPrice: this.goodPrice,
          goodDetails: details,
        })
        .then((res) => {
          if (res.success) {
            alert("添加商品成功");
            this.reset();
          } else {
            alert(res.msg);
          }
        })
        .catch((err) => {
          alert(err);
        });
    },
    handleDetailChange(file, fileList) {
      //   if (!fileList) return;
      //   this.detailList.splice(0, this.detailList.length);
      //   fileList.forEach((ele) => {
      //     if (ele && ele.response) this.detailList.push(ele.response.data);
      //   });
    },
    handleDetailSuccess(res, file) {
      this.detailList.push(res.data);
    },
    handleGoodSuccess(res, file) {
      console.log(res, file);
      this.goodImage = res.data;
    },
    handleDetailPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    handleRemoveDetail(file) {
      var index = this.detailList.indexOf(file.response.data);
      if (index >= 0) this.detailList.splice(index, 1);
      console.log(this.detailList);
    },
    home() {
      this.$router.push("/admin");
    },
    logout() {
      this.$router.push("/");
    },
    search() {
      this.$apiFun
        .manageData({})
        .then((response) => {
          if (response["success"]) {
            if (this.users.length > 0) this.users.splice(0, this.users.length);
            response.data.users.forEach((ele) => {
              this.users.push(ele);
            });
            if (this.goods.length > 0) this.goods.splice(0, this.goods.length);
            response.data.goods.forEach((ele) => {
              this.goods.push(ele);
            });
            if (this.comments.length > 0)
              this.comments.splice(0, this.comments.length);
            response.data.comments.forEach((ele) => {
              this.comments.push(ele);
            });
          } else {
            alert(response["msg"]);
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
  },
};
</script>
<style>
.image {
  width: 200px;
  height: 200px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
    